package com.cyys.modules.core.controller;

import com.cyys.modules.core.shiro.token.manage.TokenManager;
import com.cyys.modules.system.model.user.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Action超类 提供session request response ajax支持
 * 
 * @author LC
 *
 */
public class BaseAction {

	protected static final String LIST = "list";

	protected HttpServletRequest request;

	protected HttpServletResponse response;

	protected HttpSession session;

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.session = request.getSession(true);
	}


	protected User getUser() {
		return TokenManager.getToken();
	}

	protected boolean isSystemDeveloper() {
		return getUser().getId().equals("1")?true:false;
	}

	protected ServletContext getServletContext() {
		return request.getSession().getServletContext();
	}

	protected Boolean checkCode(String captcha) {
		String sessionCode = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		captcha = captcha.toLowerCase();
		if (captcha != null && captcha.equals(sessionCode)) {
			return true;
		} else {
			return false;
		}
	}

	private class MyDateEditor extends PropertyEditorSupport {
		private final boolean allowEmpty;

		public MyDateEditor(boolean allowEmpty) {
			this.allowEmpty = allowEmpty;
		}
		@Override
		public void setAsText(String text) throws IllegalArgumentException{
			SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
			if (this.allowEmpty && !StringUtils.hasText(text)) {
				this.setValue((Object)null);
			} else {
				try{
					this.setValue(dateFormat1.parse(text));
				}catch (ParseException e1){
					try{
						this.setValue(dateFormat2.parse(text));
					}catch (ParseException e2){
						try {
							this.setValue(dateFormat3.parse(text));
						} catch (ParseException e3) {
							throw new IllegalArgumentException("Could not parse date: " + e3.getMessage(), e3);
						}
					}
				}
			}

		}
	}

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		/*//只能单种时间格式转换
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));*/
		binder.registerCustomEditor(Date.class, new MyDateEditor(true));
	}
}
